package com.zhuna.www.service.impl;

import com.zhuna.www.dao.UserDAO;
import com.zhuna.www.dao.impl.UserDAOImp1;
import com.zhuna.www.po.User;
import com.zhuna.www.service.UserService;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImp1 implements UserService {
    private final UserDAO userDAO = new UserDAOImp1();

    @Override
    public int register(User user) throws SQLException {
        //先检查用户是否存在
        User existingUser = userDAO.selectUserByName(user.getName());
        if (existingUser != null) {
            return -1; // 用户已存在
        }
        return userDAO.add(user);
    }

    @Override
    //用户登录
    public User login(String name, String password) throws SQLException {
        User user = userDAO.selectUserByName(name);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }

        return null;
    }

    @Override
    //用户更新
    public int updateUser(int userId, String newName, String newPassword) throws SQLException {
        return userDAO.updateUser(userId, newName, newPassword);
    }

    @Override
    //用户删除
    public int deleteUser(String name) throws SQLException {
        return userDAO.deleteUser(name);
    }

    @Override
//    获得所有用户
    public List<User> getAllUsers() throws SQLException {
        return userDAO.selectallUser();
    }

    @Override
    //查询所有用户信息
    public User getUserByName(String name) throws SQLException {
        return userDAO.selectUserByName(name);
    }

    @Override
    //获取用户余额
    public double getWalletBalanceByName(String name) throws SQLException {
        return userDAO.getWalletBalanceByname(name);
    }


}
